#!/usr/bin/python
# -*- coding: utf-8 -*-

from Tkinter import *           # 导入 Tkinter 库
import sqlite3
src = "./snsc.db"
# src = "./snsctest.db"
class dbfile():
    def __init__(self):
        self.src = src
        # self.src = "./snsctest.db"
        self.cx = sqlite3.connect(self.src)
        self.cu = self.cx.cursor()

    def inser(self,pid,name,sid):
        self.cu.execute('INSERT INTO files (name,parentid,sort) VALUES("' + name + '",' + str(pid) + ','+str(sid)+')')
        # self.cu.execute('INSERT INTO files (name,parentid,sort,settime) VALUES("'+name+'",'+str(pid)+','+str(sid)+',datetime("now","localtime"),)')
        self.cx.commit()
    def addsid(self,sid):
        self.cu.execute("update files set sort=sort+1 where sort>" + str(sid))
        self.cx.commit()
    def search(self):
        result = self.cu.execute("select id,name,parentid,sort,ordli from files order by sort")
        return result

    def search_maxid(self):
        result = self.cu.execute("select max(id) from files").fetchone()
        return result

    def update(self,id,name):
        print name
        self.cu.execute("update files set name='"+name+"' where id="+str(id))
        self.cx.commit()

    def delt(self,id):
        self.cu.execute("delete from files where id = "+str(id))
        self.cx.commit()

class dbmes():
    def __init__(self):
        self.src = src
        self.cx = sqlite3.connect(self.src)
        self.cu = self.cx.cursor()

    def inser(self, pid, content, sid):
        self.cu.execute('INSERT INTO message (content,parentid,sort) VALUES("'+content+'",'+str(pid)+','+str(sid)+')')
        self.cx.commit()
    def addsid(self,sid):
        self.cu.execute("update message set sort=sort+1 where sort > " + str(sid))
        self.cx.commit()
    def search(self):
        result = self.cu.execute("select id,content,parentid,sort from message order by sort")
        return result
    def search_maxid(self):
        result = self.cu.execute("select max(id) from message").fetchone()
        return result

    def update(self, id, content):
        self.cu.execute("update message set content='"+content+"' where id="+str(id))
        self.cx.commit()

    def delt(self, id):
        self.cu.execute("delete from message where id = "+str(id))
        self.cx.commit()

# print int(-2)
# db = dbmes()
# # db = dbfile()
# max = db.search_maxid()[0]
# print max

# db.update(6,"6哈哈哈")
# db.delt(17)
# db.inser('''jdgfld
# dfgfddfg''',12,56)
# db.inser()
# db.inser()
# '''
# create table files_2(id INTEGER primary key,content BLOB, parentid INTEGER,sort INTEGER, settime DATETIME DEFAULT (datetime('now','localtime')))
# '''






